package Inputs;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import gui.DisplayCanvas;



public class ImageLoader {
	 private  BufferedImage loadImg;
	 private static int actual;
	 private static File[] selectedFiles;
	 private static String directory;
	 
	public ImageLoader(){
		actual = 0;
		loadImg = null;
		directory="c:";
	}

	public void setLoadImg(BufferedImage loadImg) {
		this.loadImg = loadImg;
	}

	public static BufferedImage loadImage(JFrame frame) {  
		JFileChooser filechooser = new JFileChooser();
		filechooser.setCurrentDirectory(new File(directory));
		FFilter jpgFilter, bmpFilter, pngFilter,imgFilter,bothFilter; 
		jpgFilter = new FFilter("jpg", "JPEG Imege");
		bmpFilter = new FFilter("bmp", "Bitmap Image");
		imgFilter = new FFilter("img", "Img Image");
		pngFilter = new FFilter("png", "Png Image");
		bothFilter = new FFilter(new String[] {"jpg","bmp","img","png"}, "Images");
		filechooser.setAcceptAllFileFilterUsed(true);
		filechooser.addChoosableFileFilter(jpgFilter);
		filechooser.addChoosableFileFilter(bmpFilter);
		filechooser.addChoosableFileFilter(imgFilter);
		filechooser.addChoosableFileFilter(pngFilter);
		filechooser.addChoosableFileFilter(bothFilter);
		
		filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		filechooser.setMultiSelectionEnabled(true);
	
		BufferedImage bimg = null; 
		actual=0;
 		if (filechooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
			selectedFiles=filechooser.getSelectedFiles();
			String[] path = selectedFiles[actual].toURI().getPath().split("/");
			String aux="";
			for (int i = 1; i < path.length-1; i++) {
				aux=aux + path[i] + "\\";
			}
			directory=aux;
			Image image = new ImageIcon(selectedFiles[actual].getPath()).getImage();
			bimg = new BufferedImage(image.getWidth(null), image.getHeight(null),
			    BufferedImage.TYPE_INT_RGB);
			
			 Graphics2D g = bimg.createGraphics();
			    g.drawImage(image, 0, 0, null); 
			    g.dispose(); 
		}
		return bimg; 
      } 
	 
	  public void loadAndDisplayImage(DisplayCanvas panel,JFrame frame) {	
		  	loadImg = loadImage(frame);
		  	if(loadImg!=null){
		    	 panel.setimage(loadImg);
		    	 panel.repaint();
		  	}
	  }  
	  
	  public void displayImage(DisplayCanvas panel){
		  try {
			loadImg = ImageIO.read(selectedFiles[actual]);
		} catch (IOException e) {}   
		  if(loadImg!=null){
		    	 panel.setimage(loadImg);
		    	 panel.repaint();
		     } 
	  }

	  public BufferedImage getImage(){
		  return loadImg;
	  }
	
	  public File getFileImage(){
		  return selectedFiles[actual];
		}
	  
	  public void incActual(){
		  if(actual<selectedFiles.length-1)
			  actual++;
		  else
			  actual=0;
	  }
	  public void decActual(){
		  if(actual>0)
			  actual--;
		  else
			  actual=selectedFiles.length-1;
	  }
	  
	  /**
		 * Este metodo crea una imagen y la retorna a
		 * partir del path.
		 * @param path
		 * @return ImageIcon
		 */
		public ImageIcon createImageIcon(String path) {
	
				return new ImageIcon(path);
			}
}
